package servlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.AddMasterImpl;
import model.AddUserImpl;
import model.AssignUserImpl;
import model.PollUserImpl;
import bean.PollUser;
import java.sql.Date;

public class AssignUserServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost (request, response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("GBK");
		System.out.println("here");
		String remove = request.getParameter("remove");
		if(remove == null){
			String userlist = request.getParameter("userlist");
			System.out.println("users:"+request.getParameter("userlist").toString());
			String listname = request.getParameter("partlist");
			
			String[] users = userlist.split(",");
			AssignUserImpl impl;
			String masterUsername = request.getSession().getAttribute("username").toString();
			for(int i = 1; i < users.length; i++){
				String curr = users[i];
				System.out.println("user:*"+curr+"*");
				impl = new AssignUserImpl("0", listname, masterUsername, curr);
				impl.assign();
			}
		}else{
			System.out.println("split:"+request.getParameter("partlist"));
			String removelist = request.getParameter("removelist");
			System.out.println("users:"+request.getParameter("removelist").toString());
			String listname = request.getParameter("partlist");
			String[] users = removelist.split(",");
			AssignUserImpl impl;
			String masterUsername = request.getSession().getAttribute("username").toString();
			for(int i = 1; i < users.length; i++){
				String curr = users[i];
				impl = new AssignUserImpl("0", listname, masterUsername, curr);
				impl.remove();
			}
		}
		request.getSession().setAttribute("PMUserAssignSuccess", true);
		response.sendRedirect("pmAssignUser.jsp");
	}
}
